const jwt = require('jsonwebtoken'); //token生成，解析
const secret = require('../config/index.js').jwt_secret; //token生成密钥
function decodeHander(token) {
    return new Promise(async (resolve, reject) => {
        jwt.verify(token, secret, function (err, data) {
            if (err) return reject(err)
            resolve(data)
        })
    })
}
const permission = async (req, res, next) => {
    if (req.headers.token && req.method != 'GET') {
        try {
            let data = await decodeHander(req.headers.token)
        } catch (error) {
            let resBody = {
                code:'-2',
                message:'验证失败，请重新登录'
            }
            res.status(400).json(resBody);
        }
    }
    next()
}
module.exports = permission